package com.hrabun.goodmorningalarm;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

	public DBHelper(Context context) {
		super(context, Global.DB_NAME, null, 12);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d(Global.LOG_TAG, "--- onCreate database ---");
		createAlarmsDB(db);
		createStationsDB(db);
		insertStations(db);
//		createFKAlarmsStations(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(Global.LOG_TAG, "oldVersion = " + oldVersion
				+ ", current version = " + newVersion);
		Log.d(Global.LOG_TAG, "--- onUpdate database ---");
		createStationsDB(db);
		insertStations(db);
	}
	
	private void createAlarmsDB(SQLiteDatabase db) {
		db.execSQL("create table " + Global.DB_TABLE_NAME + " ("
				+ "_id integer primary key autoincrement,"
				+ Global.DB_COLUMN_ALARM_NAME + " text,"
				+ Global.DB_COLUMN_ALARM_TIME + " text,"
				+ Global.DB_COLUMN_ALARM_ITEM_MONDAY + " integer,"
				+ Global.DB_COLUMN_ALARM_ITEM_TUESDAY + " integer,"
				+ Global.DB_COLUMN_ALARM_ITEM_WEDNESDAY + " integer,"
				+ Global.DB_COLUMN_ALARM_ITEM_THURSDAY + " integer,"
				+ Global.DB_COLUMN_ALARM_ITEM_FRIDAY + " integer,"
				+ Global.DB_COLUMN_ALARM_ITEM_SATURDAY + " integer,"
				+ Global.DB_COLUMN_ALARM_ITEM_SUNDAY + " integer,"
				+ Global.DB_COLUMN_ALARM_ITEM_RADIO + " text,"
				+ Global.DB_COLUMN_ACTIVE + " integer" + ");");
	}
	
//	private void createFKAlarmsStations(SQLiteDatabase db) {
//		db.execSQL("ALTER TABLE "+Global.DB_TABLE_NAME+" ADD COLUMN "+Global.DB_COLUMN_ALARM_ITEM_RADIO+" text");
//	}
	
	private void createStationsDB(SQLiteDatabase db) {
		db.beginTransaction();
		Log.d("MY", "DROP STATIONS TABLE");
		db.execSQL("DROP TABLE IF EXISTS "+ Global.DB_TABLE_STATIONS_NAME);
		db.setTransactionSuccessful();
		db.endTransaction();	
		
		db.beginTransaction();
		Log.d("MY", "CREATE STATIONS TABLE");
		db.execSQL("CREATE TABLE " + Global.DB_TABLE_STATIONS_NAME + " ("
				+ "_id integer primary key autoincrement,"
				+ Global.DB_COLUMN_RADIO_STATION_NAME + " text,"
				+ Global.DB_COLUMN_RADIO_STATION_URL + " text" + ");");
		db.setTransactionSuccessful();
		db.endTransaction();
	}
	
	private void insertStations(SQLiteDatabase db) {
		db.delete(Global.DB_TABLE_STATIONS_NAME, null, null);
		Log.d("MY", "INSERT STATIONS");
		/* insert stations*/ 
		ContentValues cv = new ContentValues();
		
		db.beginTransaction();
		cv.put(Global.DB_COLUMN_RADIO_STATION_NAME, "Club Dubstep");
		cv.put(Global.DB_COLUMN_RADIO_STATION_URL, "http://listen.di.fm/public3/clubdubstep.pls");
		db.insert(Global.DB_TABLE_STATIONS_NAME, null, cv);
		cv.clear();
		db.setTransactionSuccessful();
		db.endTransaction();
		
		db.beginTransaction();
		cv.put(Global.DB_COLUMN_RADIO_STATION_NAME, "Trance");
		cv.put(Global.DB_COLUMN_RADIO_STATION_URL, "http://listen.di.fm/public3/trance.pls");
		db.insert(Global.DB_TABLE_STATIONS_NAME, null, cv);
		cv.clear();
		db.setTransactionSuccessful();
		db.endTransaction();
				
		db.beginTransaction();
		cv.put(Global.DB_COLUMN_RADIO_STATION_NAME, "Club Sounds");
		cv.put(Global.DB_COLUMN_RADIO_STATION_URL, "http://listen.di.fm/public3/club.pls");
		db.insert(Global.DB_TABLE_STATIONS_NAME, null, cv);
		cv.clear();
		db.setTransactionSuccessful();
		db.endTransaction();
		
		db.beginTransaction();
		cv.put(Global.DB_COLUMN_RADIO_STATION_NAME, "Chillout");
		cv.put(Global.DB_COLUMN_RADIO_STATION_URL, "http://listen.di.fm/public3/chillout.pls");
		db.insert(Global.DB_TABLE_STATIONS_NAME, null, cv);
		cv.clear();
		db.setTransactionSuccessful();
		db.endTransaction();
		
		db.beginTransaction();
		cv.put(Global.DB_COLUMN_RADIO_STATION_NAME, "Ambient");
		cv.put(Global.DB_COLUMN_RADIO_STATION_URL, "http://listen.di.fm/public3/ambient.pls");
		db.insert(Global.DB_TABLE_STATIONS_NAME, null, cv);
		cv.clear();
		db.setTransactionSuccessful();
		db.endTransaction();
		
		db.beginTransaction();
		cv.put(Global.DB_COLUMN_RADIO_STATION_NAME, "Cosmic Downtempo");
		cv.put(Global.DB_COLUMN_RADIO_STATION_URL, "http://listen.di.fm/public3/cosmicdowntempo.pls");
		db.insert(Global.DB_TABLE_STATIONS_NAME, null, cv);
		cv.clear();
		db.setTransactionSuccessful();
		db.endTransaction();
		
		db.beginTransaction();
		cv.put(Global.DB_COLUMN_RADIO_STATION_NAME, "Techno");
		cv.put(Global.DB_COLUMN_RADIO_STATION_URL, "http://listen.di.fm/public3/techno.pls");
		db.insert(Global.DB_TABLE_STATIONS_NAME, null, cv);
		cv.clear();
		db.setTransactionSuccessful();
		db.endTransaction();
		
		db.beginTransaction();
		cv.put(Global.DB_COLUMN_RADIO_STATION_NAME, "Space Dreams");
		cv.put(Global.DB_COLUMN_RADIO_STATION_URL, "http://listen.di.fm/public3/spacemusic.pls");
		db.insert(Global.DB_TABLE_STATIONS_NAME, null, cv);
		cv.clear();
		db.setTransactionSuccessful();
		db.endTransaction();

		db.beginTransaction();
		cv.put(Global.DB_COLUMN_RADIO_STATION_NAME, "Chillout Dreams");
		cv.put(Global.DB_COLUMN_RADIO_STATION_URL, "http://listen.di.fm/public3/chilloutdreams.pls");
		db.insert(Global.DB_TABLE_STATIONS_NAME, null, cv);
		cv.clear();
		db.setTransactionSuccessful();
		db.endTransaction();
	}

}
